<?php


namespace app\index\model;


use think\Db;
use think\Model;

class Official extends Model
{
    /**
     * 用于人事审核页，获取应聘人的基本信息
     * ywh开发
     * @param $human
     * @param $state
     * @param $currPage
     * @return \think\db\Query[]|\think\Paginator
     * @throws \think\exception\DbException
     */
    public function getExamineTo($human,$state,$currPage = 1){
        return Db::table('official')
            ->alias('off')
            ->join(['examine' => 'examine'] , 'examine.official_id = off.official_id')
            ->join(['user' => 'user'] , 'user.user_id = examine.user_id')
            ->join(['userstatus' => 'state'],'state.user_id = user.user_id')
            ->field(['examine.state'])
            ->field(['user.header' , 'user.username' , 'user.workplace' , 'state.status' , 'state.work_alltime' , 'user.phone' , 'user.email' , 'state.advantage'])
            ->field('(UNIX_TIMESTAMP(NOW()) - birthday) / 31536000 as age')
            ->where(['off.user_id' => $human , 'examine.state'       =>  $state])
            ->paginate(8,false,['page' => $currPage]);
    }

    /**
     * 人事审核页中，修改投档用户的状态
     * ywh开发
     *
     * @param $applyPhone
     * @param $commit
     * @return bool
     */
    public function upExamine($applyPhone,$commit){
        if ($commit == 1){
            $action = Db::table('examine')
                ->join(['user' => 'user'],'examine.user_id = user.user_id')
                ->where('user.phone' ,'=' , $applyPhone)
                ->update(['examine.state' => 1]);
        }else{
            $action = Db::table('examine')
                ->join(['user' => 'user'],'examine.user_id = user.user_id')
                ->where('user.phone' ,'=' , $applyPhone)
                ->setInc('state',1);
        }
        if (!$action) return false;
        return true;
    }

    /**
     * 显示发布中的职位：查询相应字段
     * @copyright hyw
     * @param $companyId
     * @return Official[]|\think\Collection
     */
    public function getOffAttr($companyId) {
        return $this
            ->alias('o')
            ->where('company_id','=',$companyId)
            ->field(['o.official','job_money'])
            ->where('is_delete','=',0)
            ->select();
    }

    /**
     * 显示被删除职位的接口：查询相应的字段
     * @copyright hyw
     * @param $companyId
     * @return Official[]|\think\Collection
     */
    public function getOffDAttr($companyId) {
        return $this
            ->alias('o')
            ->where('company_id','=',$companyId)
            ->field(['o.official','job_money'])
            ->where('is_delete','=',1)
            ->select();
    }

    /**
     * 显示职位分类和职位
     * @copyright hyw
     * @param $phone
     * @return Official[]|array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getClassOAttr() {
        return $this
            ->alias('o')
            ->join(['classification' => 'cls'],'cls.official_id = o.official_id')
            ->field(['class','official'])
            ->where('cls.is_delete','=',0)
            ->where('o.is_delete','=',0)
            ->select();
    }

    /**连接user表，外键公司的id，判断当前的用户，连接职位分类表，外键职位id
     * 连接公司表以及公司类型表，外键公司id，查询相关字段
     * @copyright hyw
     * @param $phone
     * @return Official[]|array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHotJobAttr() {
        return $this
            ->alias('o')
            ->join(['classification' => 'cls'],'cls.official_id = o.official_id')
            ->where('cls.is_delete','=',0)
            ->join(['company' => 'com'],'com.company_id = o.company_id')
            ->where('com.is_delete','=',0)
            ->join(['cotype' => 'cot'],'cot.company_id = o.company_id')
            ->where('cot.is_delete','=',0)
            ->field(['o.official_id','class','official','company_area','job_money','education','wordweek',])
            ->field(['company_welfare','company_logo','company_name','type'])
            ->order('hot','desc')
            ->limit(6)
            ->select();
    }

    /**
     *职位详情信息
     * @copyright hyw
     * @param $phone
     * @return Official|array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetailAttr($officialId) {
        return $this
            ->alias('o')
            ->where('o.official_id','=',$officialId)
            ->join(['user' => 'u'],'u.user_id = o.user_id')
            ->join(['company' => 'com'],'com.company_id = o.company_id')
            ->where('com.is_delete','=',0)
            ->join(['cotype' => 'cot'],'cot.company_id = o.company_id')
            ->join(['comassage' => 'ms'],'ms.company_id = o.company_id')
            ->field(['official_id,official','off_area','job_money','wordweek','education','company_welfare'])
            ->field(['u.user_id,header','username','position'])
            ->field(['requirement','official_describe'])
            ->field(['company_logo','company_name','type','com_official','company_introduction'])
            ->field(['company_name','company_addtime','add_money','username','type','management','company_area'])
            ->select();
    }


    public function getSearchOfficialAttr($searchValue,$page=1,$searchEducation='',$searchBackground='',$searchPeople='',$searchMoney='',$SearchType='',$province='',$urban='',$town='')
    {
         return $this->table('official')
            ->alias('off')
            ->join('company company','off.company_id=company.company_id')
            ->join('classification class','off.official_id=class.official_id')
            ->join('user user','off.user_id=user.user_id')
            ->join('cotype t','off.company_id= t.company_id')
            ->where('off.is_delete','=','0')
            ->where('company.is_delete','=','0')
            ->where('off.official|off.education|company.company_name|t.type','like','%'.$searchValue.'%')
            ->where('.off.education','like','%'.$searchEducation.'%')
            ->where('off.worktime','like','%'.$searchBackground.'%')
            ->where('company.com_official','like','%'.$searchPeople.'%')
            ->where('t.type','like','%'.$SearchType.'%')
            ->where('off.job_money','like','%'.$searchMoney.'%')
            ->where('off.off_area','like','%'.$province.'%')
            ->where('off.off_area','like','%'.$urban.'%')
            ->where('off.off_area','like','%'.$town.'%')
            ->field('off.official_id,off.official,off.off_area,off.job_money,company.company_id,company.company_name,off.requirement,off.wordweek,off.education,t.type,company.company_logo,user.username,user.header,company.company_welfare,company.com_official')
            ->field('company.*')
            ->select();
    }

    public function checkSearchOfficial($searchValue,$page=1,$searchEducation='',$searchBackground='',$searchPeople='',$searchMoney='',$SearchType='',$province='',$urban='',$town='')
    {
            return $this
            ->alias('off')
            ->join('company company','off.company_id=company.company_id')
            ->join('classification class','off.official_id=class.official_id')
            ->join('user user','off.user_id=user.user_id')
            ->join('cotype t','off.company_id= t.company_id')
            ->where('off.is_delete','=','0')
            ->where('company.is_delete','=','0')
            ->where('off.official|off.education|company.company_name|t.type','like','%'.$searchValue.'%')
            ->where('.off.education','like','%'.$searchEducation.'%')
            ->where('off.worktime','like','%'.$searchBackground.'%')
            ->where('company.com_official','like','%'.$searchPeople.'%')
            ->where('t.type','like','%'.$SearchType.'%')
            ->where('off.job_money','like','%'.$searchMoney.'%')
            ->where('off.off_area','like','%'.$province.'%')
            ->where('off.off_area','like','%'.$urban.'%')
            ->where('off.off_area','like','%'.$town.'%')
            ->field('off.official_id,off.official,off.off_area,off.job_money,company.company_name,off.requirement,off.wordweek,off.education,t.type,company.company_logo,user.username,user.header,company.company_welfare,company.com_official')
            ->select();
    }

    public function getCompanyPosition($companyId)
    {
        return $this
            ->alias('o')
            ->join('company c','c.company_id=o.company_id')
            ->where('c.company_id','=',$companyId)
            ->where('c.is_delete','=','0')
            ->field('o.*')
            ->select();
    }

    /**
     * 获取职位的部分相关信息
     * @copyright hyw
     * @param $companyId
     * @param $official
     * @param $jobMoney
     * @return Official
     */
    public function getOffIdAttr($companyId,$official,$jobMoney) {
        return $this
            ->where('company_id','=',$companyId)
            ->where('official','=',$official)
            ->where('job_money','=',$jobMoney)
            ->field('official_id')
            ->find();
    }

    public function hotSearch()
    {
        return $this->field('official')->orderRand()->limit(8)->select();
    }

    /**
     * @param int $page
     * @param string $searchEducation
     * @param string $searchBackground
     * @param string $searchPeople
     * @param string $searchMoney
     * @param string $SearchType
     * @param string $province
     * @param string $urban
     * @param string $town
     * @return Official|\think\Paginator
     * ljj 返回全部职位信息
     */
    public function searchOfficialAllBack($page=1,$searchEducation='',$searchBackground='',$searchPeople='',$searchMoney='',$SearchType='',$province='',$urban='',$town='')
    {
        return $this
            ->alias('off')
            ->join('company company','off.company_id=company.company_id')
            ->join('classification class','off.official_id=class.official_id')
            ->join('user user','off.user_id=user.user_id')
            ->join('cotype t','off.company_id= t.company_id')
            ->where([
                ['.off.education','like','%'.$searchEducation.'%'],
                ['off.worktime','like','%'.$searchBackground.'%'],
                ['company.com_official','like','%'.$searchPeople.'%'],
                ['company.com_official','like','%'.$searchPeople.'%'],
                ['t.type','like','%'.$SearchType.'%'],
                ['off.job_money','like','%'.$searchMoney.'%'],
                ['off.off_area','like','%'.$province.'%'],
                ['off.off_area','like','%'.$urban.'%'],
                ['off.off_area','like','%'.$town.'%']
            ])
            ->field('off.official_id,off.official,off.off_area,off.job_money,company.company_id,company.company_name,off.requirement,off.wordweek,off.education,t.type,company.company_logo,user.username,user.header,company.company_welfare,company.com_official')
            ->paginate(15,['page' =>$page]);
    }
}
